package dao;
import entity.User;
import utils.JdbcUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class UserDao {
    public static boolean existsUsername(String name) {
        String sql = "select * from USERS where UNAME = ?";
        try (Connection conn = JdbcUtils.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, name);
            ResultSet rs = ps.executeQuery();
            return rs.next();
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void addUser(User user) {
        JdbcUtils jdbcUtils = new JdbcUtils();
        try{jdbcUtils.executeTransaction((connection, ps) ->{
            String sql = "insert into USERS(UNAME,USEX,PNAME,SUSERS) values(?,?,?,?)";
            ps = connection.prepareStatement(sql);
            ps.setString(1,user.getName());
            ps.setString(2, user.getSex());
            ps.setString(3, user.getPname());
            ps.setString(4,user.getSuperusers());

            int rs = ps.executeUpdate();
            if(rs>0) {
                System.out.println("添加成功");
            }else {
                System.out.println("添加失败");
            }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void deleteUser(String username) throws SQLException {
        JdbcUtils jdbcUtils = new JdbcUtils();
        try{jdbcUtils.executeTransaction((connection, ps) ->{
            String sql = "insert into USERS(UNAME) values(?)";
            ps = connection.prepareStatement(sql);
            ps.setString(1,username);

            int rs = ps.executeUpdate();
            if(rs>0) {
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
        });
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void reviseUser(User user) {
       JdbcUtils jdbcUtils = new JdbcUtils();
        try{
            jdbcUtils.executeTransaction((connection, ps) ->{
                String sql = "UPDATE USERS set USEX=?,PNAME=?,SUSERS=? WHERE UNAME=?";
                ps = connection.prepareStatement(sql);
                ps.setString(1,user.getSex());
                ps.setString(2,user.getPname());
                ps.setString(3,user.getSuperusers());
                ps.setString(4,user.getName());
                int rs = ps.executeUpdate();
                if(rs>0) {
                    System.out.println("修改成功");
                }else {
                    System.out.println("修改失败");
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void inquireAll() {
        String sql = "select U_ID,UNAME,USEX,PNAME,SUSERS from USERS  ";
        try(Connection connection= JdbcUtils.getConnection();
            PreparedStatement pstmt=connection.prepareStatement(sql)){
            ResultSet rs=pstmt.executeQuery();
            while(rs.next()) {
                int id = rs.getInt("U_ID");
                String uname = rs.getString("UNAME");
                String sex = rs.getString("USEX");
                String pname = rs.getString("PNAME");
                String superusers = rs.getString("SUSERS");
                User user = new User(id,uname, sex, pname, superusers);
                System.out.println(user);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    public static void inquiry(String name) throws SQLException {
        String sql = "select U_ID,UNAME,USEX,PNAME,SUSERS from USERS WHERE UNAME=?  ";
        try(Connection connection= JdbcUtils.getConnection();
            PreparedStatement pstmt=connection.prepareStatement(sql)){
            pstmt.setString(1,name);
            ResultSet rs=pstmt.executeQuery();
            while(rs.next()) {
                int id = rs.getInt("U_ID");
                String uname = rs.getString("UNAME");
                String sex = rs.getString("USEX");
                String pname = rs.getString("PNAME");
                String superusers = rs.getString("SUSERS");
                System.out.println("用户ID：" + id +
                        "  用户姓名："+uname +
                        "  用户性别：" +sex+
                        "  用户选择项目："+pname+
                        "  用户是否会员："+superusers);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}



